package com.jed.xxh.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.jed.xxh.application.BaseApplication;

/**
 * 项目名：MyApplication<br/>
 * 包名：  com.jed.xxh.utils<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/24 17:09<br/>
 * 描述：SharedPreferences轻量级存储工具类<br/>
 * put+类型：存储数据<br/>
 * get+类型：获取数据
 */
public class ShareUtil {

    private static final String NAME = "config";

    public static void putString(String key, String value) {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().putString(key, value).commit();
    }

    public static String getString(String key, String defValue) {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    public static void putInt(String key, int value) {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(String key, int defValue) {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public static void putBoolean(String key, Boolean value) {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().putBoolean(key, value).commit();
    }

    public static Boolean getBoolean(String key, Boolean defValue) {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }

    /**
     * 删除单个信息
     *
     * @param key
     */
    public static void deleShare(String key) {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().remove(key).commit();
    }

    /**
     * 删除所有信息
     */
    public static void deleAll() {
        SharedPreferences sp = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

    /**
     * 获取系统上下文
     *
     * @return
     */
    public static Context getContext() {
        return BaseApplication.getAppContext();
    }

}
